﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CoreMobile.cmd;
using TestingMobileNet.cmd;
using TestingMobileNet.log;
using CoreMobile.net;
using CoreMobile.csv;

namespace TestingMobileNet
{
    public partial class TesterUI : Form
    {
        private const int MAX_DATA_SIZE = 5;

        private List<TMobileNetData> list;

        public TesterUI()
        {
            InitializeComponent();

            list = TMobileNetData.generateSequenciableData(MAX_DATA_SIZE);
            progressBar.Minimum = 0;
            progressBar.Maximum = MAX_DATA_SIZE;
            progressBar.Value = 0;
           // progressBar = 1;


            Client.DEFAULT_IP = "192.168.0.40";
            Client.DEFAULT_SEND_PORT = 8081;
        }

        private void exitItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void startItem_Click(object sender, EventArgs e)
        {
            var cachedList = new List<string[]>(MAX_DATA_SIZE);

            foreach (var elem in list)
            {
                this.send(elem);
                this.updateUI(elem);
                cachedList.Add(elem.info());
            }

            var fileName = @"\My Documents\testing-net.csv";

            string str = CSVDocument.exportData(new[] {"TMobileNet"}, cachedList, fileName);
            if (str == null)
                MessageBox.Show("Datos exportados en: "+fileName);
            else
                MessageBox.Show("Reporte este mensaje: "+ str);
        }

        private void send(TMobileNetData elem)
        {
            if (! Client.sendQuickly(new VABProtocol().commandSend(new TMobileNetDataCmd(), elem)))
            {
                debugText.Text += "NONET";
                elem.Id += "-NONET";
            }
        }

        private void updateUI(TMobileNetData elem)
        {

            debugText.Text += elem.Id + "-" + elem.NetworkQuality + Environment.NewLine;
            updateProgressBar();
        }

        private void updateProgressBar()
        {
            progressBar.Value = progressBar.Value + 1;
            progressBar.Update();
        }
    }
}